//
//  MineCtrl.m
//  Traveling
//
//  Created by Northwan on 15/5/12.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//  我的页面

#import "MineCtrl.h"
#import "RegisterCtr.h"
#import "LoginCtrl.h"
#import "MyOrdersCtrl.h"
#import "MyTripOrdersCtrl.h"
#import "BusinessLoginCtr.h"


#define BTNINSERT 15
#define BTNHEIGHT 40
@interface MineCtrl ()
{
    UIButton *_tripBtn;
    UIButton *_orderBtn;
    UIButton *_loginBtn;
    UIButton *_businessBtn;
    UIButton *_registBtn;
}
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIImageView *headImgView;
@property (weak, nonatomic) IBOutlet UIView *containerView;
@end

@implementation MineCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    [_scrollView setAlwaysBounceVertical:YES];

    UIButton *tripBtn =  [self setupBtn:@"我的行程单"  tag:0];
    _tripBtn = tripBtn;
    [self setBtnFrame:tripBtn theLastView:self.headImgView];
    
    UIButton *orderBtn =  [self setupBtn:@"我的订单"  tag:1];
    _orderBtn=orderBtn;
    [self setBtnFrame:orderBtn theLastView:_tripBtn];
    
    UIButton *loginBtn =  [self setupBtn:@"登录"  tag:2];
     _loginBtn= loginBtn;
      [self setBtnFrame:loginBtn theLastView:_orderBtn];
    
    UIButton *businessBtn =  [self setupBtn:@"商户登录"  tag:3];
     _businessBtn = businessBtn;
    [self setBtnFrame:businessBtn theLastView:_loginBtn];
    
    UIButton *registBtn =  [self setupBtn:@"注册"  tag:4];
     _registBtn = registBtn;
    [self setBtnFrame:registBtn theLastView:_businessBtn];

}


-(UIButton *)setupBtn:(NSString *)title tag:(NSInteger)tag
{
    UIButton *btn = [[UIButton alloc]init];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [btn setTag:tag];
    [btn addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
    [self dealBtnBorder:btn];
    [self.containerView addSubview:btn];
    return btn;
}

-(void)setBtnFrame:(UIButton*)btn theLastView:(UIView *) theLastView{
    
    [btn setFrame:CGRectMake(BTNINSERT, CGRectGetMaxY(theLastView.frame)+BTNINSERT, SCREENW-BTNINSERT * 2, BTNHEIGHT)];
    
}

-(void)dealBtnBorder:(UIButton*)btn{
    [btn.layer setCornerRadius:5];
    [btn.layer setBorderColor:RGBColor(231, 231, 231).CGColor];
    [btn.layer setBorderWidth:1];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    if (isLogin) {
        [_registBtn setTitle:@"退出" forState:UIControlStateNormal];
    }
    
    
    NSInteger flag = [DefaultsHelper integerForKey:kCurrentIdentiryID];
   
    if (flag == companyIdentityType){
        [_businessBtn setHidden:YES];
        [self updateBtnFrame:NO];
    }else if(flag == personIdentityType){
        [_loginBtn setHidden:YES];
        [self updateBtnFrame:YES];
     }
}


-(void)updateBtnFrame:(BOOL)status{
    if(status){
        [_businessBtn setY:CGRectGetMaxY(_orderBtn.frame)+BTNINSERT];
        [_registBtn setY:CGRectGetMaxY(_businessBtn.frame)+BTNINSERT];
    }else{
         [_registBtn setY:CGRectGetMaxY(_loginBtn.frame)+BTNINSERT];
    }
}

- (void)tapAction:(UIButton *)sender {
    
    switch (sender.tag) {
        case 0:
            NSLog(@"我的行程单");
            if (!isLogin) {
               [DialogUtils showTips:@"您还未登录!"];
                return;
            }
            [self.navigationController pushViewController:[[MyTripOrdersCtrl alloc] init ]animated:YES];
            break;
        case 1:
            NSLog(@"我的订单");
            if (!isLogin) {
                [DialogUtils showTips:@"您还未登录!"];
                return;
            }
            [self.navigationController pushViewController:[[MyOrdersCtrl alloc] init ]animated:YES];
            break;
            
        case 2:
            NSLog(@"登陆");
            [self.navigationController pushViewController:[[LoginCtrl alloc] init ]animated:YES];
            break;
        case 3:
             NSLog(@"商户登录");
            [self.navigationController pushViewController:[[BusinessLoginCtr alloc] init] animated:YES];
            break;
        case 4:
            NSLog(@"注册");
            if ([_registBtn.titleLabel.text isEqualToString:@"退出"]) {
                [self ExitClick:nil];
            }else{
                 [self.navigationController pushViewController:[[RegisterCtr alloc] init ]animated:YES];
            }
            break;

        default:
            break;
    }
}

-(void)ExitClick:(UIButton *)btn{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"确定退出吗？" message:@"" delegate: nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
        alert.delegate = self;
        [alert show]; //显示退出对话框
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [self cleanInfo];
        [self reloadView];
    }
}

- (void)reloadView
{
    [self setBtnFrame:_loginBtn theLastView:_orderBtn];
    [self setBtnFrame:_businessBtn theLastView:_loginBtn];
    [self setBtnFrame:_registBtn theLastView:_businessBtn];
    [_registBtn setHidden:NO];
    [_registBtn setTitle:@"注册" forState:UIControlStateNormal];
    [_loginBtn setHidden:NO];
    [_businessBtn setHidden:NO];
}

-(void)cleanInfo{
    [DefaultsHelper setObject:nil forKey:kNickName];
    [DefaultsHelper setObject:nil forKey:kUserId];
    [DefaultsHelper setObject:nil forKey:kCurrentIdentiryID];
}

@end
